# global.gd
extends Node

@export var max_damage: int = 5
@export var max_health: int = 100
@export var now_health: int = 100
@export var speed: float = 200.0
@export var ex: int = 0
var mouse_world_position: Vector2 = Vector2.ZERO
var camera_2d: Camera2D = null
var 经验条
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var main_node = get_tree().get_root().get_node("main")
	经验条=main_node.get_node_or_null("摄像机控件/Camera2D/经验条")
	initialize()
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	经验条.update_from_global()

# 初始化函数，用于查找 Camera2D 节点。
func initialize() -> void:
	var main_node = get_tree().get_root().get_node_or_null("main") 
	if main_node:
		var camera_controller_node = main_node.get_node_or_null("摄像机控件")
		if camera_controller_node:
			camera_2d = camera_controller_node.get_node_or_null("Camera2D")
	
	if camera_2d == null:
		printerr("Global script failed to find Camera2D node at path 'main/摄像机控件/Camera2D'!")

# 更新全局鼠标世界坐标。
func update_mouse_world_position() -> void:
	if camera_2d == null:
		return
	var mouse_screen_pos = get_viewport().get_mouse_position()
	mouse_world_position = camera_2d.get_canvas_transform().affine_inverse() * mouse_screen_pos
